using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using MotionFramework.MVC;
using System;

namespace MotionFramework.UIKit
{
    /// <summary>
    /// 这里强制把 MVC Mediator 继承 MonoBehaviour
    /// 即可实现 MonoBehaviour 和 ViewComponent 融合
    /// 中小型项目感觉用不到这么多耦合拆解
    /// 常规项目 Model ModelProxy MonoBehaviour Mediator Command
    /// 简化项目 ModelProxy MonoBehaviour Command
    /// 优点原来5个文件现在3个文件即可实现功能模块
    /// 耦合性在可接受范围
    /// </summary>
    public class UIMediator : MonoBehaviour, IMediator, INotifier
    {
        /// <summary>the mediator name</summary>
        public string MediatorName { get; protected set; }

        /// <summary>The view component</summary>
        public object ViewComponent { get; set; }

        /// <summary>Return the Singleton Facade instance</summary>
        protected IFacade Facade => MotionFramework.MVC.Facade.GetInstance(() => new MotionFramework.MVC.Facade());

        /// <summary>
        /// 是否存在注册消息
        /// </summary>
        /// <returns></returns>
        protected bool HasNotificationInterests => ListNotificationInterests().Length > 0;

        /// <summary>
        /// 是否注册View
        /// </summary>
        protected bool IsRegister = false;

        /// <summary>
        /// 组件被创建时
        /// </summary>
        protected virtual void Awake()
        {
            // 尽早执行初始化UI组件
            InitializeComponent();
        }

        /// <summary>
        /// 注册视图消息
        /// </summary>
        protected virtual void RegisterViewCommand()
        {
            if (!IsRegister && HasNotificationInterests)
            {
                // 注册前赋值
                MediatorName = GetType().Name;
                ViewComponent = this;
                Facade.RegisterMediator(this);
                IsRegister = true;
            }
        }

        /// <summary>
        /// 销毁视图消息
        /// </summary>
        protected virtual void DestroyViewCommand()
        {
            if (IsRegister)
            {
                // 移除
                Facade.RemoveMediator(GetType().Name);
                IsRegister = false;
            }
        }

        /// <summary>
        /// 消息接收者
        /// </summary>
        /// <param name="notification"></param>
        public virtual void HandleNotification(INotification notification)
        {
        }

        /// <summary>
        /// 注册视图消息
        /// </summary>
        /// <returns></returns>
        public virtual string[] ListNotificationInterests()
        {
            return new string[0];
        }

        /// <summary>
        /// 注册回调
        /// </summary>
        public virtual void OnRegister()
        {
        }

        /// <summary>
        /// 移除回调
        /// </summary>
        public virtual void OnRemove()
        {
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="notificationName"></param>
        /// <param name="body"></param>
        /// <param name="type"></param>
        public virtual void SendNotification(string notificationName, object body = null, string type = null)
        {
            Facade.SendNotification(notificationName, body, type);
        }

        #region UI 查找相关

        /// <summary>
        /// 获取UI控件 - 自动绑定功能
        /// </summary>
        protected virtual void InitializeComponent() {}

        /// <summary>
        /// 查找节点 - 名称
        /// </summary>
        protected Transform Find(string n)
        {
            return Prefab.Find(transform, n);
        }

        /// <summary>
        /// 获取节点 - 索引
        /// </summary>
        protected Transform GetChild(int index)
        {
            return Prefab.GetChild(transform, index);
        }

        /// <summary>
        /// 点击事件
        /// </summary>
        protected void AddClickEventListener(string n, Action onClick)
        {
            Prefab.AddClickEventListener(transform, n, onClick);
        }

        /// <summary>
        /// 点击事件
        /// </summary>
        protected void AddClickEventListener(Transform transform, Action onClick)
        {
            Prefab.AddClickEventListenerByTransform(transform, onClick);
        }

        /// <summary>
        /// 点击事件
        /// </summary>
        protected void AddClickEventListener(GameObject gameObject, Action onClick)
        {
            Prefab.AddClickEventListenerByGameObject(gameObject, onClick);
        }

        /// <summary>
        /// 点击事件
        /// </summary>
        protected void AddClickEventListener(Button component, Action onClick)
        {
            component?.onClick.AddListener(() => {
                onClick?.Invoke();
            });
        }

        /// <summary>
        /// 点击事件
        /// </summary>
        protected void AddClickEventListener(Toggle component, Action<bool> onClick)
        {
            component?.onValueChanged.AddListener((enabled) => {
                    onClick?.Invoke(enabled);
                });
        }

        #endregion
    }
}
